// 1. src/store/reducer/todo.js reducer函数
const initState = {
  list: [
    { id: 1, name: '学习vue2', done: true },
    { id: 2, name: '学习vue3', done: true },
    { id: 3, name: '学习react', done: false },
  ],
};

export const todoReducer = (state = initState, action) => {
  console.log('action -----> ', action);
  switch (action.type) {
    case 'edit':
      return {
        ...state,
        list: state.list.map((item) => {
          return {
            ...item,
            done: action.payload === item.id ? !item.done : item.done,
          };
        }),
      };

    case 'del':
      return {
        ...state,
        // id相同的要删掉，id不同的要留下
        list: state.list.filter((item) => item.id !== action.payload),
      };

    case 'add':
      return {
        ...state,
        list: [action.payload, ...state.list],
      };

    case 'changeAll':
      return {
        ...state,
        list: state.list.map((item) => {
          return { ...item, done: action.payload };
        }),
      };

    default:
      return state;
  }
};
